package com.ehotting.edsta.mqbd.circuit.client.config;

import com.ehotting.edsta.mqbd.circuit.client.handler.CircuitConsumerResponseAction;
import com.ehotting.edsta.mqbd.circuit.client.handler.CircuitSendUserServiceAble;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.core.config.MqbdCorePropertyConfig;
import com.ehotting.mqbd.core.helper.MQStringHelper;
import com.ehotting.mqbd.core.logger.MqMsgLogger;
import com.ehotting.mqbd.core.mq.consumer.MessageConsumer;
import com.ehotting.mqbd.core.manager.ModuleManager;
import com.ehotting.mqbd.core.sync.MqSyncFutureMapHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
@DependsOn("mqbdCorePropertyConfig")
public class CircuitConsumerResponseConfig {

    @Bean
    public CircuitConsumerResponseAction circuitConsumerResponseAction(CircuitSendUserServiceAble circuitSendUserService,
                                                                  MqSyncFutureMapHelper mqSyncFutureMapHelper,
                                                                  MqApplicationContext mqApplicationContext,
                                                                  ModuleManager moduleManager,
                                                                  MqMsgLogger mqMsgLogger){
        CircuitConsumerResponseAction handler = new CircuitConsumerResponseAction(
                mqSyncFutureMapHelper,
                mqApplicationContext,
                TopicEnum.CONCURRENCY,
                DirectionEnum.RESPONSE,
                null,
                moduleManager,
                mqMsgLogger);
        handler.setCircuitSendUserService(circuitSendUserService);
        return handler;
    }

    @Bean(name="circuitConcurrencyResponseMessageConsumer",initMethod = "start",destroyMethod = "shutdown")
    public MessageConsumer circuitConcurrencyResponseMessageConsumer(MqbdCorePropertyConfig mqbdCorePropertyConfig,
                                                                CircuitConsumerResponseAction circuitConsumerResponseAction,
                                                                MqApplicationContext mqApplicationContext){
        MessageConsumer messageConsumer = new MessageConsumer(
                mqbdCorePropertyConfig.getConsumer(),
                MQStringHelper.createConsumerGroupAtConsumerApp(TopicEnum.CONCURRENCY,
                        mqApplicationContext,
                        DirectionEnum.RESPONSE),
                circuitConsumerResponseAction);
        return messageConsumer;
    }

}